<# 在 UNIX 环境下挂载 FAT32 镜像，将测试程序写入镜像中 #>
Push-Location
Set-Location $PSScriptRoot/build

if ([String]::IsNullOrEmpty($args))
{
    throw '$TEST_PROGRAM cannot be NULL.'
}
if (($PSVersionTable).Platform -eq "Win32NT")
{
    try
    {
        $WSL = Get-Command -Name "wsl"
        $dir = New-Guid
        WSL -u root -- mkdir /mnt/$dir
        WSL -u root --cd $PWD -- mount SimpleOS.img /mnt/$dir
        WSL -u root --cd /mnt/$dir -- mkdir -p PROGRAM
        WSL -u root --cd $PWD -- cp $args /mnt/$dir/PROGRAM
        WSL -u root -- umount /mnt/$dir
        WSL -u root -- rm -r /mnt/$dir
    }
    catch
    {
        Write-Error $_
    }
}
else # UNIX
{   
    $dir = New-Guid
    sudo mkdir /mnt/$dir
    sudo mount SimpleOS.img /mnt/$dir
    sudo mkdir -p /mnt/$dir/PROGRAM
    sudo cp $args /mnt/$dir/PROGRAM
    sudo umount /mnt/$dir
    sudo rm -r /mnt/$dir
}
Pop-Location